<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//内容控制器
class ContentController extends Controller{
	//构造函数
	public function __init()
	{
		
	}
	
    //动作
    public function index(){
    	$goods_id = Q('get.gid',0,'intval'); 	
       //文章表数据
       $goodsData = Db::table('goods')
	                ->join('detail','goods_id','=','detail_goods_id')
                    ->where('goods_id',$goods_id)->first();
	   $goodsData['small'] = explode('|', $goodsData['small']);
	   $goodsData['big'] = explode('|', $goodsData['big']);
//	   p($goodsData);      
	   //规格表数据
	   $type_id = Db::table('goods_attr')->join('typeattr','goodsattr_typeattr_id','=',"typeattr_id")->where('goodsattr_goods_id',$goods_id)->where('typeattr_type',1)->pluck('typeattr_type_id');
	   $goodsattrData = Db::table('typeattr')->where('typeattr_type',1)->where('typeattr_type_id',$type_id)->get();
	   foreach($goodsattrData as $k=>$v)
	   {
	   	$goodsattrData[$k]['value'] = Db::table('goods_attr')->where('goodsattr_typeattr_id',$v['typeattr_id'])->where('goodsattr_goods_id',$goods_id)->get();
	   }
//	   p($goodsattrData);
       $count = count($goodsattrData);
	   //接收异步消息
	   if(IS_AJAX)
	   {
	   	$tname = "";
	   	foreach($goodsattrData as $k=>$v)
		{
			$tname[] = $v['typeattr_name'];
		}
	   	$spec = $_POST['spec'];
		$spec = rtrim($spec,",");
		$num = $_POST['num'];
		$data = [
		     "id" => $goods_id,
		     'name' => $goodsData['goods_name'],
		     'num' => $num,
		     'price' => $goodsData['shopprice'],
		     'options' => $spec,
		     'tname' => $tname,
		     'image' => $goodsData['pic'],
		];
		Cart::add($data);//添加到购物车
		echo json_encode($phpdata=1);
		die;
	   }
	   View::with('count',$count);
	   View::with('goodsattrData',$goodsattrData);
	   View::with('goodsData',$goodsData);	   
       View::make();
    }
    public function getAjax(){
    	if(IS_AJAX)
		{
			$gid = $_POST['gid'];
			$gid = rtrim($gid,',');
			$total = Db::table('goods_list')->where('combine',$gid)->pluck('goodslist_total');
			echo json_encode($total);
		}
    }
	public function buyAjax()
	{
		$goods_id = Q('get.gid',0,'intval');
		//文章表数据
       $goodsData = Db::table('goods')
	                ->join('detail','goods_id','=','detail_goods_id')
                    ->where('goods_id',$goods_id)->first();
		//规格表数据
	   $type_id = Db::table('goods_attr')->join('typeattr','goodsattr_typeattr_id','=',"typeattr_id")->where('goodsattr_goods_id',$goods_id)->where('typeattr_type',1)->pluck('typeattr_type_id');
	   $goodsattrData = Db::table('typeattr')->where('typeattr_type',1)->where('typeattr_type_id',$type_id)->get();
	   //接收异步消息
	   if(IS_AJAX)
	   {
	   	unset($_SESSION['order']);
	   	$tname = "";
	   	foreach($goodsattrData as $k=>$v)
		{
			$tname[] = $v['typeattr_name'];
		}
	   	$spec = $_POST['spec'];
		$spec = rtrim($spec,",");
		$spec = explode(',', $spec);
		foreach($spec as $k=>$v)
		{
			$options[] = $v;
		}
		$num = $_POST['num'];
		$total = (int)$num * (int)$goodsData['shopprice'];
		$data = [
		     "id" => $goods_id,
		     'name' => $goodsData['goods_name'],
		     'num' => $num,
		     'price' => $goodsData['shopprice'],
		     'options' => $options,
		     'tname' => $tname,
		     'image' => $goodsData['pic'],
		     'total' => $total,
		];
		$_SESSION['order'][0] = array(
		     "id" => $goods_id,
		     'name' => $goodsData['goods_name'],
		     'num' => $num,
		     'price' => $goodsData['shopprice'],
		     'options' => $options,
		     'tname' => $tname,
		     'image' => $goodsData['pic'],
		     'total' => $total,
		);
		
		echo json_encode($phpdata=1);
		die;
	   }
	}
}
